
App.Router.map(function() {
        
    this.resource("users", function() {
        this.resource('user', {path: '/:id' });
    });
    
    this.resource("teams", function() {
        this.resource('team', {path: '/:id' });
    });

    this.resource("fields", function() {
        this.resource('field', {path: '/:id' });
    });

    this.resource("requests", function() {
        this.resource('request', {path: '/:id' });
    });
        
    this.route('about');
    this.route('login');
    this.route('register');
    this.route('error');
    
    this.route('createuser');
    this.route('createteam');
    this.route('createfield');

    this.route('catchAll', { path: "/*path" });
});

// ---------------------------------------

App.AuthRoute = Ember.Route.extend({
    beforeModel: function(transition) {
        var self = this;
        var tok = this.controllerFor('login').get('token');
        $.post(App.Config.server, {
            token:  tok,
            action: 'check'
        }, "json").
        done(function(data) {
            var cont = $.parseJSON(data);
            /* XXXXX
            if (cont.response !== 'true') {
                self.controllerFor('login').set('error', "Log in to see this content");
                self.transitionTo('login');
            }*/
        });
    },
    actions: {        
        error: function(reason, transition) {
            if (reason.status === 401) {
                this.transitionTo('error');
            }
            this.transitionTo('error');
        }
    }    
});

App.FieldsRoute = App.AuthRoute.extend({
    model: function() {
        return this.store.find(App.Field, null);
    }
});

App.FieldRoute = App.AuthRoute.extend({
    model: function(f) {
        return this.store.find(App.Field, f);
    }
});

App.UsersRoute = App.AuthRoute.extend({
    model: function() {
        return this.store.find(App.User, null);
    }
});

App.UserRoute = App.AuthRoute.extend({
    /*
    setupController: function(controller, u) {
        controller.set('user', u);      
        controller.set('userinfo', 'GGGG');/*
 * function(u) {
            var obj = {id: u.get('userinfo_id')};
            return this.store.find(App.Userinfo, obj); 
        }.property('userinfo'));
        
    }    
    model: function(u) {
        return Ember.RSVP.hash({
            user: this.store.find(App.User, u),
            userinfo: this.store.find(App.Userinfo, {id: 
                        4
          //    u.get('userinfo_id')
          })
        })
    }*/
    model: function(u) {
        return this.store.find(App.User, u);
    }
});

App.TeamsRoute = App.AuthRoute.extend({
    model: function() {
        return this.store.find(App.Team, null);
    } 
});

App.TeamRoute = App.AuthRoute.extend({
   model: function(t) {
       return this.store.find(App.Team, t);
   } 
});

App.LoginRoute = App.AuthRoute.extend({
   setupController: function(controller, context) {
       //controller: reset();
   } 
});
